package com.caidanxiaoyuan.controller;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;

import com.caidanxiaoyuan.model.JifenList;
import com.caidanxiaoyuan.model.ShengHuoDingdanList;
import com.caidanxiaoyuan.model.ShengHuoList;
import com.caidanxiaoyuan.model.ShopList;
import com.caidanxiaoyuan.model.ShopListView;
import com.caidanxiaoyuan.model.SongdaShijianConfig;
import com.caidanxiaoyuan.model.UserList;
import com.caidanxiaoyuan.model.WebConfig;
import com.caidanxiaoyuan.service.JifenListService;
import com.caidanxiaoyuan.service.ShengHuoDingdanListService;
import com.caidanxiaoyuan.service.ShengHuoListService;
import com.caidanxiaoyuan.service.SongDaShiJianConfigService;
import com.caidanxiaoyuan.service.UserListService;
import com.caidanxiaoyuan.service.WebConfigService;
import com.caidanxiaoyuan.util.JsonUtil;
import com.caidanxiaoyuan.util.ResponseUtil;
import com.github.pagehelper.Page;

import ShopListMapper.ShopListService;


@Controller
@CrossOrigin
@RequestMapping("/shenghuo")
public class ShengHuoController {

	@Autowired
	private SongDaShiJianConfigService songDaShiJianConfigService;
	@Autowired
	private WebConfigService webConfigService;
	@Autowired
	private ShengHuoListService shengHuoListService;
	@Autowired
	private ShengHuoDingdanListService shengHuoDingdanListService;
	@Autowired
	private JifenListService jifenListService;
	@Autowired
	private UserListService userListService;
	@Autowired
	private ShopListService shopListService;
	
	/**
	 * 
	 * @Description : 送货时间
	 * @author zealot
	 * @param @param page
	 * @param @param response
	 * @return void
	 * @version 创建时间 : 2017年2月22日 上午10:18:08
	 * @thorws
	 */
	@RequestMapping("/shijian")
	public void shijian(Page<SongdaShijianConfig> page,HttpServletResponse response){
		ResponseUtil.result(response, JsonUtil.toJsonStr(songDaShiJianConfigService.selectAll(page)));
	}
	
	/**
	 * 
	* @Description: 评价  
	* @author zealot
	* @param @param e
	* @param @param response 
	* @return void    
	* @version 创建时间：2017年2月28日 上午11:04:06  
	* @throws
	 */
	@RequestMapping("/pingjia")
	public void pingjia(ShengHuoDingdanList e,HttpServletResponse response){
		e.setState(new Byte("3"));
		shengHuoDingdanListService.updateByPrimaryKey(e);
		JifenList jf = new JifenList();
		jf.setMyid(e.getMyid());
		jf.setDingdanid(e.getDizhiid());
		short jifen = (short) (e.getJianyi()==null?2:5);
		jf.setJifen(jifen);
		jf.setShijian((int) Long.parseLong(String.valueOf(System.currentTimeMillis()).toString().substring(0,10)));
		jifenListService.insert(jf);
		jf = jifenListService.sum(e.getMyid());
		UserList user = new UserList();
		user.setMyid(e.getMyid());
		user.setJifen((int) (jf.getJifen()>0?jf.getJifen():0));
		userListService.updateByPrimaryKey(user);
	}
	
	/**
	 * 
	* @Description: 我发布的产品  
	* @author zealot
	* @param @param page
	* @param @param response
	* @param @param t 
	* @return void    
	* @version 创建时间：2017年2月28日 下午3:12:36  
	* @throws
	 */
	@RequestMapping("/shop")
	public void shop(Page<ShopList> page,HttpServletResponse response,ShopList t){
		ResponseUtil.result(response, JsonUtil.toJsonStr(shopListService.selectAll(page, t)));
	}
	
	@RequestMapping("/xianshi")
	public void xianshi(Page<WebConfig> page,HttpServletResponse response){
		List<WebConfig> list =  webConfigService.selectAll(page);
		if(list!=null){
			if(list.size()>0){
				ResponseUtil.result(response, JsonUtil.toJsonStr(list.get(0)));
			}
		}
	}
	
	@RequestMapping("/view")
	public void view(ShengHuoList t,HttpServletResponse response){
		ResponseUtil.result(response, JsonUtil.toJsonStr(shengHuoListService.selectByPrimaryKey(t.getId())));
	}
	
	
	@RequestMapping("/shenghuo")
	public String shenghuo(ShengHuoList t,HttpServletRequest request){
		t = shengHuoListService.selectByPrimaryKey(t.getId());
		request.setAttribute("e", t);
		return "h5/shenghuo";
	}
	
	/**
	 * 
	* @Description: 生活列表页  
	* @author zealot
	* @param @param t
	* @param @param response 
	* @return void    
	* @version 创建时间：2017年3月6日 下午5:21:27  
	* @throws
	 */
	@RequestMapping("/list")
	public void list(Page<ShengHuoList> page,HttpServletResponse response,ShengHuoList t){
		Map map = new HashMap();
		
		Page<ShengHuoList> list = (Page<ShengHuoList>) shengHuoListService.selectAll(page);
		map.put("data",list.getResult());
		map.put("totalpage", list.getPages());
		map.put("totalrows", list.getTotal());
		
		ResponseUtil.result(response, JsonUtil.toJsonStr(map));
	}
	
	@RequestMapping("/dingdan")
	public void dingdan(ShengHuoDingdanList t,HttpServletResponse response){
		ShengHuoList sh = shengHuoListService.selectByPrimaryKey(t.getShenghuoid());
		if(sh!=null){
			if(sh.getFenleiid()==1){
				if(sh.getKucun()<t.getShuliang()){
					Map p = new HashMap();
					p.put("code", 17);
					ResponseUtil.result(response, JsonUtil.toJsonStr(p));
				}
				int jf = sh.getPayjifen()*t.getShuliang();
				t.setUsejifen(new Byte(String.valueOf(jf)));
			}else{
				t.setUsejifen(new Byte("0"));
				double f = Math.random()*(sh.getMaxjifen()-sh.getMinjifen())+sh.getMinjifen();
				f = new BigDecimal(f).setScale(0, RoundingMode.UP).doubleValue();
				t.setGetjifen((byte) f);
			}
			long sj = Long.parseLong(String.valueOf(System.currentTimeMillis()).toString().substring(0,10));
			t.setShijian((int) sj);
			int id = shengHuoDingdanListService.insert(t);
			Map map = new HashMap();
			map.put("dingdanid", t.getId());
			ResponseUtil.result(response, JsonUtil.toJsonStr(map));
		}
	}
	
	/**
	 * @throws Exception 
	 * 
	* @Description: 砸彩蛋  
	* @author zealot
	* @param @param e 
	* @return void    
	* @version 创建时间：2017年3月1日 上午9:55:14  
	* @throws
	 */
	@RequestMapping("/jifen")
	public void jifen(ShengHuoDingdanList e,HttpServletResponse response) throws Exception{
		e = shengHuoDingdanListService.selectByPrimaryKey(e.getOrderid());
		if(e.getGetjifen()!=0){
			JifenList jf = new JifenList();
			jf.setMyid(e.getMyid());
			jf.setDingdanid(e.getOrderid());
			short jifen = (short)e.getGetjifen();
			jf.setJifen(jifen);
			jf.setShijian((int) Long.parseLong(String.valueOf(System.currentTimeMillis()).toString().substring(0,10)));
			jifenListService.insert(jf);
			jf = jifenListService.sum(e.getMyid());
			UserList user = new UserList();
			user.setMyid(e.getMyid());
			user.setJifen((int) (jf.getJifen()>0?jf.getJifen():0));
			userListService.updateByPrimaryKey(user);
			ResponseUtil.write(response, e.getGetjifen());
		}else{
			ResponseUtil.write(response, 0);
		}
	}
}
